//
//  BasePC.m
//  BaseCode
//
//  Created by 曹连华 on 2020/3/24.
//

#import "BasePC.h"
#import "BaseAppDelegate.h"
#import <CoreCode/CoreCode.h>

@interface BasePC ()

@property(nonatomic, strong) UIView *dimmingView;

@end

@implementation BasePC

- (void)loadView {
    [super loadView];

    if (!self.dimmingView) {
        self.dimmingView = [UIView autolayoutView];
        self.dimmingView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5f];
        self.dimmingView.alpha = 0.f;
    }
    [self.view addSubview:self.dimmingView];
    [CoreConstraintHelper setView:self.dimmingView fullAsSuperview:self.view andEdgeInsets:self.dimmingEdge];

    [self.dimmingView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapDimmingView)]];
}

- (void)onTapDimmingView {
    WEAK_SELF;
    if ([self.pcDelegate respondsToSelector:@selector(shouldDismissPresentationController:)]) {
        STRONG_SELF;
        if (![self.pcDelegate shouldDismissPresentationController:self]) {
            return;
        }
    }

    [self dismissViewControllerAnimated:YES completion:^{
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    }];

}

- (NSTimeInterval)duration {
    if (_duration == 0.f) {
        _duration = .4f;
    }

    return _duration;
}

- (void)presentViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    BaseAppDelegate *appDelegate = (BaseAppDelegate *) [UIApplication sharedApplication].delegate;
    if (appDelegate.modalWindow.rootViewController.childViewControllers.count) {
        appDelegate.modalWindow.hidden = NO;
        [appDelegate.modalWindow makeKeyWindow];
    }

    if ([self.pcDelegate respondsToSelector:@selector(willPresentPC:)]) {
        [self.pcDelegate willPresentPC:self];
    }

    //call viewwillappear
    [self callChildVCsViewWillAppear:animated];

    WEAK_SELF;
    [UIView animateWithDuration:self.duration animations:^{
        STRONG_SELF;
        self.dimmingView.alpha = 1.f;
        self.animationVC.view.center = CGPointMake(self.animationVC.view.center.x, self.animationVC.view.center.y - self.animationVC.view.bounds.size.height);
        self.animationView.center = CGPointMake(self.animationView.center.x, self.animationView.center.y - self.animationView.bounds.size.height);
    }                completion:^(BOOL finished) {
        STRONG_SELF;
        [self didMoveToParentViewController:self.parentViewController];
        [self.animationVC didMoveToParentViewController:self.animationVC.parentViewController];

        //call viewdidappear
        [self callChildVCsViewDidAppear:animated];

        if ([self.pcDelegate respondsToSelector:@selector(didPresentPC:)]) {
            [self.pcDelegate didPresentPC:self];
        }

        if (completion) {
            completion();
        }
    }];
}

- (void)dealFinishedDismissViewControllerAnimated:(BOOL)animated {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];

    [self.animationVC removeFromParentViewController];

    //call viewdiddisappear
    [self callChildVCsViewDidDisappear:animated];
    //取消显示pc window
    BaseAppDelegate *appDelegate = (BaseAppDelegate *) [UIApplication sharedApplication].delegate;
    if (appDelegate.modalWindow.rootViewController.childViewControllers.count == 0) {
        [appDelegate.modalWindow endEditing:YES];//隐藏pc的键盘
        appDelegate.modalWindow.hidden = YES;
    }

    if ([self.pcDelegate respondsToSelector:@selector(didDismissPC:)]) {
        [self.pcDelegate didDismissPC:self];
    }

}

- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [super dismissViewControllerAnimated:animated completion:completion];

    if ([self.pcDelegate respondsToSelector:@selector(willDismissPC:)]) {
        [self.pcDelegate willDismissPC:self];
    }

    [self willMoveToParentViewController:nil];

    //call viewwilldisappear
    [self callChildVCsViewWillDisappear:animated];

    if (animated) {
        WEAK_SELF;
        [UIView animateWithDuration:self.duration animations:^{
            STRONG_SELF;
            self.dimmingView.alpha = 0.f;
            self.animationVC.view.center = CGPointMake(self.animationVC.view.center.x, self.animationVC.view.center.y + self.animationVC.view.bounds.size.height);
            self.animationView.center = CGPointMake(self.animationView.center.x, self.animationView.center.y + self.animationView.bounds.size.height);
        }                completion:^(BOOL finished) {
            STRONG_SELF;
            [self dealFinishedDismissViewControllerAnimated:animated];
            if (completion) {
                completion();
            }
        }];
    } else {
        [self dealFinishedDismissViewControllerAnimated:animated];
        if (completion) {
            completion();
        }
    }
}

//是否是pc
- (BOOL)isPc {
    return YES;
}

@end
